<?

class file extends Think {
	
	static function is_empty($path) {
		$i = 0;
		$d = opendir ( $path );
		while ( $f = readdir ( $d ) ) {
			$i ++;
		}
		closedir ( $d );
		return $i > 2 ? 0 : 1;
	}
	
	static function list_file($path, $type = 'all') {
		$list = array ();
		$ob_dir = opendir ( $path );
		while ( $file = readdir ( $ob_dir ) ) {
			if ($file == "." || $file == "..") {
				continue;
			}
			
			if ($type == 'all') {
				
				$list [] = $file;
			} elseif ($type == 'dir') {
				
				if (is_dir ( $file )) {
					$list [] = $file;
				}
			} elseif ($type == 'file') {
				
				if (is_file ( $file )) {
					$list [] = $file;
				}
			}
			
		}
		closedir ( $ob_dir );
		return $list;
	}
	
	static function del_dir($path, $del_me = 1) {
		//import('app.ORG.encode');
		if(!is_dir($path)){
			return false;
		}
		if (file::is_empty ( $path ) && $del_me == 1) {
			//是空的
			rmdir ( $path );
		} else {
			//不是空的
			$d = dir ( $path );
			while ( $f = $d->read () ) {
				//echo $path . '/' .encode::encode_change($f).'<br>'.$del_me.'<br>';
				if ($f == "." || $f == "..") {
					continue;
				} elseif (is_dir ( $path . '/' . $f )) {
					//如果是目录
					if (! file::is_empty ( $path . '/' . $f )) {
						//调用自身
						file::del_dir ( $path . '/' . $f );
					}
					if($del_me || ($path . '/' . $f) != $path) {
						rmdir ( $path . '/' . $f );
					}
				
				} elseif (is_file ( $path . '/' . $f )) {
					//文件直接删除
					unlink ( $path . '/' . $f );
				}
			}
			$d->close ();
			
			if ($del_me == 1) {
				rmdir ( $path );
			}
		}
		return 1;
	}
	
	static function del_file($url) {
		if (file_exists ( $url )) {
			return unlink ( $url );
		} else {
			return "file not here";
		}
	}
}
?>